#include <iostream>
#include <mysql/mysql.h>
#include <string>
#include <unistd.h>
using namespace std;
const std::string host = "localhost";
const std::string user = "dgz";
const std::string passwd = "Waiwzj#789";
const std::string db = "cdata";
const unsigned int port = 3306;

int main()
{

    //std::cout << "mysql version:" << mysql_get_client_info() << std::endl;
    MYSQL* my = mysql_init(nullptr);
    if(my == nullptr)
    {
        cerr << "init error" << endl;
        exit(1);
    }
    mysql_set_character_set(my,"utf8");
    if (mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        cerr << "mysql_real_connect error" << endl;
        exit(2);
    }
    cout << "mysql_real_connect success" << endl;
    int n = mysql_query(my,"select * from user");
    MYSQL_RES* mr = mysql_store_result(my);
    if(mr == nullptr)
    {
        cerr << "select error" << endl;
        exit(3);
    }
    my_ulonglong row = mysql_num_rows(mr);
    unsigned int colnum = mysql_num_fields(mr);
    cout << "row:" << row << "   " << "fields:" << colnum << endl;
    MYSQL_FIELD* fileds = mysql_fetch_field(mr);
    for(int i = 0;i < colnum;i++)
    {
        cout << fileds[i].name << '\t';
    }
    cout << endl;
    MYSQL_ROW line;
    for(int i = 0;i < row;i++)
    {
        line = mysql_fetch_row(mr);
        for(int j = 0;j < colnum;j++)
        {
            cout << line[j] << '\t';
        }
        cout << endl;
    }
    

    mysql_close(my);
    return 0;
}